package com.wl.controller;

import com.mysql.cj.util.StringUtils;
import com.wl.pojo.Comments;
import com.wl.response.CommonReturnType;
import com.wl.service.CommentsService;
import com.wl.service.impl.CommentsServiceImpl;
import com.wl.util.MyUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @program: treeHole
 * @author: 吴磊
 * @create: 2021-07-08 12:54
 */
@WebServlet("/comment.action")
public class CommentServlet extends BaseServlet {

    private CommentsService commentsService = new CommentsServiceImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String op = request.getParameter("op");
        if (StringUtils.isNullOrEmpty(op)) {
            show404Common(request, response);
            return;
        }
        if ("insert".equals(op)) {
            insertOp(request, response);
        } else if ("getOne".equals(op)) {
            getOneOp(request, response);
        } else if ("delete".equals(op)) {
            deleteOp(request, response);
        } else if ("commentUp".equals(op)) {
            commentUpOP(request, response);
        } else if ("getOwnerArticleComments".equals(op)) {
            getOwnerArticleCommentsOp(request, response);
        } else if ("getOwnerComments".equals(op)) {
            getOwnerCommentsOp(request, response);
        } else if ("getAllComments".equals(op)) {
            getAllCommentsOp(request, response);
        } else if ("deleteComment".equals(op)) {
            deleteCommentOp(request, response);
        } else {
            show404Common(request, response);
        }
    }

    /**
     * 获取所有评论
     *
     * @param request
     * @param response
     */

    private void getAllCommentsOp(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = commentsService.getAllComments(request);
        MyUtil.responseData(response, CommonReturnType.success(map));
    }


    /**
     * 获取自己全部的评论
     *
     * @param request
     * @param response
     */
    private void getOwnerCommentsOp(HttpServletRequest request, HttpServletResponse response) {
        boolean b = checkToken(request, response);
        if (!b) {
            MyUtil.responseData(response, CommonReturnType.fail("请登录后再执行此操作"));
            return;
        }
        Map<String, Object> map = commentsService.getOwnerComments(request);
        MyUtil.responseData(response, CommonReturnType.success(map));
    }

    /**
     * 获取自己文章的评论
     *
     * @param request
     * @param response
     */
    private void getOwnerArticleCommentsOp(HttpServletRequest request, HttpServletResponse response) {
        boolean b = checkToken(request, response);
        if (!b) {
            MyUtil.responseData(response, CommonReturnType.fail("请登录后再执行此操作"));
            return;
        }
        Map<String, Object> map = commentsService.getCommentManagementByUserId(request);
        MyUtil.responseData(response, CommonReturnType.success(map));
    }

    /**
     * 评论点赞或者取消点赞
     *
     * @param request
     * @param response
     */
    private void commentUpOP(HttpServletRequest request, HttpServletResponse response) {
        boolean b = checkToken(request, response);
        if (!b) {
            MyUtil.responseData(response, CommonReturnType.fail("请登录后再执行此操作"));
            return;
        }
        Map<String, Object> map = commentsService.commentUp(request, request.getParameter("commentId"));
        MyUtil.responseData(response, CommonReturnType.success(map));
    }

    /**
     * 根据评论Id删除评论及子评论
     *
     * @param request
     * @param response
     */
    private void deleteOp(HttpServletRequest request, HttpServletResponse response) {
        boolean b = checkToken(request, response);
        if (!b) {
            MyUtil.responseData(response, CommonReturnType.fail("请登录后再执行此操作"));
            return;
        }
        Map<String, Object> map = commentsService.deleteCommentsByCommentId(request, request.getParameter("commentId"));
        MyUtil.responseData(response, CommonReturnType.success(map));
    }

    /**
     * 管理员根据评论Id删除评论及子评论
     *
     * @param request
     * @param response
     */
    private void deleteCommentOp(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = commentsService.deleteCommentsByCommentId(request, request.getParameter("commentId"));
        MyUtil.responseData(response, CommonReturnType.success(map));
    }


    /**
     * 根据文章Id获取评论
     *
     * @param request
     * @param response
     */
    private void getOneOp(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = commentsService.getCommentByArticleId(request, request.getParameter("articleId"));
        MyUtil.responseData(response, CommonReturnType.success(map));
    }

    /**
     * 用户进行评论操作
     *
     * @param request
     * @param response
     */
    private void insertOp(HttpServletRequest request, HttpServletResponse response) {
        boolean b = checkToken(request, response);
        if (!b) {
            MyUtil.responseData(response, CommonReturnType.fail("请登录后再执行此操作"));
            return;
        }
        Comments comments = MyUtil.getTFromRequest(Comments.class, request);
        Map<String, Object> map = commentsService.insertComment(request, comments);
        if (map == null) {
            MyUtil.responseData(response, CommonReturnType.fail("此秘密已经删除"));
            return;
        }
        MyUtil.responseData(response, CommonReturnType.success(map));
    }
}
